// app.factory('websocket', function ($websocket) {

//     // Open a WebSocket connection
//     var dataStream = $websocket('ws://localhost:3001/echo');

//     // var dataStream = $websocket('ws://localhost:3000/ws-stuff/echo');

//     var data = { msg: '', merchant: '' };

//     dataStream.onOpen(function () {
//         console.log('websocket onOpen');
//         dataStream.send('client: websocket open');
//     });

//     dataStream.onMessage(function (message) {
//         console.log('onMessage', message);
//         if (message.data == 'reflash') {
//             data.msg = '你有新的订单';
//         }
//     });

//     dataStream.onError(function () {
//         console.log('websocket onError');
//     });

//     dataStream.onClose(function () {
//         console.log('websocket onClose');
//     });

//     var methods = {
//         data: data,
//         setMerchant: function (merchant) {
//             dataStream.send({ merchant: merchant });
//         }
//         // get: function () {
//         //     dataStream.send(JSON.stringify({ action: 'get' }));
//         // }
//     };

//     return methods;
// });

// app.factory('socketio', function () {
//     var socket = io.connect('http://localhost:3001');
//     var methods = {
//         setMerchant: function (rootScope) {
//             socket.emit('id', { id: rootScope.merchant._id });
//             socket.on('update', function (data) {
//                 alert('您有新的订单，请及时处理');
//                 // 更新操作
//                 rootScope.hasNewOrder.boolean = true;
//                 rootScope.newOrder.number++;
//                 alert(rootScope.hasNewOrder.boolean);
//                 alert(rootScope.newOrder.number);
//             });
//         }
//     };
//     return methods;
// });
module.exports = function(app){
    app.controller('orderCtrl', function ($scope, $rootScope, $http, socketio) {
        
            $scope.hasNewOrder = false;
            $scope.newOrder = 0;
            // socketio.setFunc(function (data) {
            //     alert('您有新的订单，请及时处理');
            //     // 更新操作
            //     $scope.hasNewOrder = true;
            //     $scope.newOrder++;
            //     alert($scope.hasNewOrder);
            //     alert($scope.newOrder);
            // });
            console.log('ffffffffffffff');
            console.log(window.updateFunc);
            console.log('ffffffffffffff');
            $scope.change = function () {
                $scope.hasNewOrder = true;
                $scope.newOrder++;
            }
            window.updateFunc = function (data) {
                alert('您有新的订单，请及时处理');
                // 更新操作
                $scope.change();
                $scope.$apply();
            };
            $scope.$watch('hasNewOrder', function () {console.log('bool change change')});
            $scope.$watch('newOrder', function () {console.log('number change change')});
            console.log('ffffffffffffff');
            console.log(window.updateFunc);
            console.log('ffffffffffffff');
        
            $scope.reflash = function () {
                $scope.sync();
            }
        
            $scope.orderByX = function (x) {
                console.log("call order by", x);
                $scope.orderBy = x;
            };
            $scope.init = function () {
                $scope.sync();
            };
            $scope.sync = function () {
                console.log("call orders sync()");
                $http({
                    method: 'GET',
                    url: 'orders'
                }).then(function successCallBack(res) {
                    console.log('callback sync() success', res);
                    $scope.orders = res.data;
                }, function errCallback(res) {
                    console.log('callback sync() error', res);
                });
                $scope.hasNewOrder = false;
                $scope.newOrder = 0;
            };
        });
}
